<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item> 书籍管理 </el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        v-if="option.column"
        :option="option"
        :data="data"
        :page.sync="page"
        v-model="form"
        :table-loading="loading"
        @on-load="getList"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @refresh-change="refreshChange"
        @search-reset="resetForm"
        @search-change="searchChange"
      >
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            icon="el-icon-info"
            type="text"
            size="small"
            @click.stop="childlist(scope)"
            >章节列表</el-button
          >
          <el-button
            icon="el-icon-s-data"
            type="text"
            size="small"
            @click.stop="scoreSet(scope.row)"
            >评分设置</el-button
          >
        </template>
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="small"
            plain
            @click="exportExcel()"
            >导出Excel</el-button
          >
          <el-button type="primary" size="small" plain @click="importBooksBtn()"
            >书籍导入</el-button
          >
          <el-button
            type="primary"
            size="small"
            plain
            @click="importRecordBtn()"
            >导入记录</el-button
          >
          <el-popover
              placement="bottom"
              width="600"
              trigger="click">
            <div class="popover-content" v-html="html"></div>
            <el-button slot="reference" type="primary" size="small" plain >书籍格式化</el-button>
          </el-popover>
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <!-- 分成比例 -->
        <template slot="platform_proportion" slot-scope="scope">
          <div class="platform_proportion">
            <input type="text" v-model="scope.row.platform_proportion" /> :
            <input type="image" v-model="platform" />
          </div>
        </template>
      </avue-crud>
    </basic-container>
    <!-- 导入书籍 -->
    <el-dialog :visible.sync="importBooksPopup" :append-to-body="true">
      <!-- 上传按钮 -->
      <!-- {{myAction}} -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div class="create_mode">
          <el-form-item label="创建方式：">
            <el-radio-group v-model="ruleForm.type" @change="changeType">
              <el-radio label="0">创建新书</el-radio>
              <el-radio label="1">章节更新</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div class="new_book_box" v-if="ruleForm.type == 0">
          <div class="cover_box">
            <el-form-item label="书籍封面：" prop="cover_url">
              <el-upload
                class="upload-demo"
                :action="myAction"
                :on-change="changeFile"
                :on-success="successimage"
                accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                :show-file-list="false"
              >
                <img
                  v-if="ruleForm.cover_url"
                  :src="ruleForm.cover_url"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="书籍名称：" prop="book_title">
                <el-input
                  v-model="ruleForm.book_title"
                  placeholder="请输入书籍名称"
                ></el-input>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs_textarea">
            <div class="dialog_title_box brief">
              <el-form-item label="书籍简介：" prop="book_intro">
                <el-input
                  type="textarea"
                  v-model="ruleForm.book_intro"
                  placeholder="请输入书籍简介"
                ></el-input>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="书籍状态：" prop="writing_process">
                <el-select v-model="ruleForm.writing_process" placeholder="请选择书籍状态">
                  <el-option
                    v-for="item in writing_process_options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="关联书籍id：">
                <el-input
                  v-model="ruleForm.book_id"
                  placeholder="请输入关联书籍id"
                ></el-input>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="语言类型" prop="language">
                <el-select v-model="ruleForm.language" placeholder="请选择语言类型">
                  <el-option
                    v-for="item in language_options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="书籍偏好：" prop="site">
                <el-select v-model="ruleForm.site" placeholder="请选择语言类型">
                  <el-option
                    v-for="item in site_options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="dialog_title_box">
              <el-form-item label="书籍分类：" prop="category_id_1">
                <el-select v-model="ruleForm.category_id_1" placeholder="请选择书籍分类">
                  <el-option
                    v-for="item in classificationList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>

          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="文件上传：" prop="fileUrl">
                <div class="dialog_title_box">
                  <div class="file_name" v-if="ruleForm.fileUrl != ''">
                    {{ fileName
                    }}<i class="el-icon-close" @click="handleRemove"></i>
                  </div>
                  <el-upload
                    v-else
                    class="upload-demo"
                    :action="myActions"
                    :on-change="changeFiles"
                    :on-success="successFile"
                    accept=".txt"
                    :limit="1"
                    :show-file-list="false"
                    :on-remove="handleRemove"
                    ref="myUpload"
                  >
                    <el-button :loading="loadingBtn" size="small" type="primary"
                      >添加文件</el-button
                    >
                  </el-upload>
                  <div class="el-upload__tip">* 仅支持txt格式</div>
                  <div class="el-upload__tip">
                    <a :href="downFile" target="downloadFile" style="text-decoration:underline;color:blue">下载txt样本</a>
                    <iframe style="display: none;" name="downloadFile"></iframe>
                  </div>
                </div>
              </el-form-item>
            </div>
          </div>
        </div>

        <!-- 更新章节 -->
        <div class="chapter_update_box" v-if="ruleForm.type == 1">
          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="书籍id：" prop="book_id">
                <el-input
                  v-model="ruleForm.book_id"
                  placeholder="请输入书籍id"
                ></el-input>
              </el-form-item>
            </div>
          </div>
          <div class="cover_boxs">
            <div class="dialog_title_box">
              <el-form-item label="书籍状态：" prop="writing_process">
                <el-select v-model="ruleForm.writing_process" placeholder="请选择书籍状态">
                  <el-option
                    v-for="item in writing_process_options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
          <div class="cover_boxs">
            <el-form-item label="文件上传：" prop="fileUrl">
              <div class="dialog_title_box">
                <div class="file_name" v-if="ruleForm.fileUrl != ''">
                  {{ fileName
                  }}<i class="el-icon-close" @click="handleRemove"></i>
                </div>
                <el-upload
                  v-else
                  class="upload-demo"
                  :action="myActions"
                  :on-change="changeFiles"
                  :on-success="successFile"
                  accept=".txt"
                  :limit="1"
                  :show-file-list="false"
                  :on-remove="handleRemove"
                  ref="myUpload"
                >
                  <el-button :loading="loadingBtn" size="small" type="primary"
                    >添加文件</el-button
                  >
                </el-upload>
                <div class="el-upload__tip">* 仅支持txt格式</div>
                <div class="el-upload__tip">
                  <a :href="downFile" target="downloadFile" style="text-decoration:underline;color:blue">下载txt样本</a>
                  <iframe style="display: none;" name="downloadFile"></iframe>
                </div>
              </div>
            </el-form-item>
          </div>
        </div>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 弹出框 -->
    <el-dialog
      :title="bookScore.book_title"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
    >
      <el-form :model="bookScore" label-width="80px">
        <el-form-item label="评分类型">
          <el-select
            v-model="bookScore.score_type"
            placeholder="请选择评分类型"
          >
            <el-option label="默认评分" :value="1"></el-option>
            <el-option label="用户评分" :value="2"></el-option>
            <el-option label="人工评分" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="整体感觉">
          <el-rate
            v-model="bookScore.whole"
            show-score
            text-color="#ff9900"
            :allow-half="true"
            :max="10"
            :score-template="bookScore.whole + ''"
          ></el-rate>
          <el-input-number
            v-model="bookScore.whole"
            placeholder="请输入分数"
            class="score-input"
            size="mini"
            :min="1"
            :max="10"
            @change="updateScore"
          ></el-input-number>
        </el-form-item>

        <el-form-item label=" 剧情好">
          <el-rate
            v-model="bookScore.plot"
            show-score
            text-color="#ff9900"
            :allow-half="true"
            :max="10"
            :score-template="bookScore.plot + ''"
          ></el-rate>
          <el-input-number
            v-model="bookScore.plot"
            placeholder="请输入分数"
            class="score-input"
            size="mini"
            :min="1"
            :max="10"
            @change="updateScore"
          ></el-input-number>
        </el-form-item>

        <el-form-item label=" 人设香">
          <el-rate
            v-model="bookScore.character_setting"
            show-score
            text-color="#ff9900"
            :allow-half="true"
            :max="10"
            :score-template="bookScore.character_setting + ''"
          ></el-rate>
          <el-input-number
            v-model="bookScore.character_setting"
            placeholder="请输入分数"
            class="score-input"
            size="mini"
            :min="1"
            :max="10"
            @change="updateScore"
          ></el-input-number>
        </el-form-item>

        <el-form-item label=" 超有梗">
          <el-rate
            v-model="bookScore.punchline"
            show-score
            text-color="#ff9900"
            :allow-half="true"
            :max="10"
            :score-template="bookScore.punchline + ''"
          ></el-rate>
          <el-input-number
            v-model="bookScore.punchline"
            placeholder="请输入分数"
            class="score-input"
            size="mini"
            :min="1"
            :max="10"
            @change="updateScore"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateScoreSubmit">修 改</el-button>
      </div>
    </el-dialog>
    <export-message :dialogVisible="dialogVisible"></export-message>
  </div>
</template>


<script>
import ExportMessage from "@/components/export/message";
import { getList, updateBook, getUploadTxt, imageUpload } from "@/api/cat/book";
import { catUrl } from "@/config/env";
import { common } from "@/api/cat/httpRequest";
import {
  AppBook as baseURL,
  bookImport,
  bookCategory,
  uploadFile,
  appChannelInfo,
  appBookScore,
} from "@/api/cat/baseUrls";
import { handleExcelCat } from "@/api/exportCommon";
export default {
  components: { ExportMessage },
  data() {
    return {
      dialogVisible: false,
      platform: 1, //平台比例
      // 新增书籍参数
      bookAdd: {
        bookCover: "",
      },
      loadingBtn: false,
      fileName: "",
      importBooksPopup: false,
      html: '' +
          '<html>\n' +
          '\t<head></head>\n' +
          '\t<body>\n' +
          '\t\t<form name="input" action="'+this.catUrl + "/bookImport/txtFormatFactory"+'" method="POST" ENCTYPE="multipart/form-data" accept-charset="UTF-8"  οnsubmit="document.charset=\'UTF-8\';">\n' +
          '\t\t\t<input type="file" name="file" single accept="text/plain">\n' +
          '\t\t\t<input type="submit" value="格式化">\n' +
          '\t\t</br>\n' +
          '\t\t</br>\n' +
          '\t\t<strong style = "color:red">该功能仅支持TXT文件！</strong></br>\n' +
          '\t\t</br>\n' +
          '\t\t<strong>输出后格式为：</strong></br>\n' +
          '\t\t<span>####章节标题</span></br>\n' +
          '\t\t<span>##正文</span></br>\n' +
          '\t\t<span>...</span></br>\n' +
          '\t\t</br>\n' +
          '\t\t<strong>目前支持转换的格式有：</strong></br>\n' +
          '\t\t<span>(1)</span></br>\n' +
          '\t\t<span>###第一章标题</span></br>\n' +
          '\t\t<span>###第二章标题</span></br>\n' +
          '\t\t<span>...</span></br>\n' +
          '\t\t<span>(2)</span></br>\n' +
          '\t\t<span>[###]1-35931449[###]Chapter 1</span></br>\n' +
          '\t\t<span>[###]1-35931449[###]Chapter 2</span></br>\n' +
          '\t\t<span>...</span></br>\n' +
          '\t</body>' +
          '</html>' +
          '',
      flagImage: false,
      ruleForm: {
        type: "0", //导入类型 0 创建新书 1 更新书籍
        cover_url: "", //书籍封面
        book_title: "", //书籍名称
        book_intro: "", //书籍简介
        writing_process: "1", //书籍状态
        book_id: "", //书籍ID
        language: "", //语言类型
        site: "", //书籍偏好
        category_id_1: "", //书籍分类
        fileUrl: "", //文件上传
      },
      rules: {
        cover_url: [
          { required: true, message: "请选择书籍封面", trigger: "blur" },
          { required: true, message: "请选择书籍封面", trigger: "change" },
        ],
        book_title: [
          { required: true, message: "请输入书籍名称", trigger: "blur" },
          { required: true, message: "请输入书籍名称", trigger: "change" },
        ],
        book_intro: [
          { required: true, message: "请输入书籍简介", trigger: "blur" },
          { required: true, message: "请输入书籍简介", trigger: "change" },
        ],
        writing_process: [
          { required: true, message: "请选择书籍状态", trigger: "blur" },
          { required: true, message: "请选择书籍状态", trigger: "change" },
        ],
        language: [
          { required: true, message: "请选择语言类型", trigger: "blur" },
          { required: true, message: "请选择语言类型", trigger: "change" },
        ],
        site: [
          { required: true, message: "请选择书籍偏好", trigger: "blur" },
          { required: true, message: "请选择书籍偏好", trigger: "change" },
        ],
        category_id_1: [
          { required: true, message: "请选择书籍分类", trigger: "blur" },
          { required: true, message: "请选择书籍分类", trigger: "change" },
        ],
        fileUrl: [
          { required: true, message: "请上传书籍", trigger: "blur" },
          { required: true, message: "请上传书籍", trigger: "change" },
        ],
        // 章节更新
        book_id: [
          { required: true, message: "请选择书籍分类", trigger: "blur" },
          { required: true, message: "请选择书籍分类", trigger: "change" },
        ],
      },
      // 书籍状态选项
      writing_process_options: [
        {
          label: "完结",
          value: "1",
        },
        {
          label: "连载",
          value: "0",
        }
      ],
      // 语言类型选项
      language_options: [
        {
          label: "中文",
          value: 1,
        },
        {
          value: 2,
          label: "英文",
        },
        {
          value: 3,
          label: "芬兰语",
        },
        {
          value: 4,
          label: "繁体",
        }
      ],
      // 书籍偏好选项
      site_options: [
        {
          label: "男频",
          value: 1,
        },
        {
          label: "女频",
          value: 2,
        },
      ],
      // 书籍分类
      category_id_1_options: [
        {
          value: 1,
          label: "是",
        },
        {
          value: 0,
          label: "否",
        },
      ],
      classificationList: [],
      imageUrl: "",
      options: [],
      fileList: [],
      value: "",
      loading: false,
      page: {
        total: 0,
      },
      channel_id: "",
      data: [],
      json: {},
      form: {},
      param: {},
      condition: [],
      category: [],
      bookScore: {
        book_title: "",
        whole: 8.5,
        plot: 8.5,
        character_setting: 8.5,
        punchline: 8.5,
        score_type: 1,
      },
      sorce_template: "10",
      dialogFormVisible: false,
      option: {
        title: "书籍列表",
        addBtn: false,
        editBtn: true,
        viewBtn: false,
        delBtn: false,
        searchBtn: true,
        refreshBtn: true,
        border: true,
        column: [
          {
            label: "书籍id",
            prop: "book_id",
            type: "input",
            span: "书籍id",
            gutter: "id",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            readonly: true,
          },
          {
            label: "cp书籍id",
            prop: "cp_book_id",
            type: "input",
            span: "cp书籍id",
            gutter: "id",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            readonly: true,
          },
          {
            label: "书籍名称",
            prop: "book_title",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "书籍名称必须填写",
              },
            ],
          },

          {
            type: "upload",
            label: "书籍封面",
            span: 24,
            prop: "cover_url",
            listType: "picture-img",
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "书籍封面必须上传",
              },
            ],
          },
          {
            label: "总章节数",
            prop: "chapter_count",
            type: "input",
            search: false,
            required: true,
            editDisplay: false,
            span: 8,
          },
          {
            label: "是否完本",
            prop: "writing_process",
            type: "select",
            editDisplay: false,
            editDisabled: true,
            search: true,
            placeholder: "是否完本",
            dicData: [
              {
                label: "是",
                value: 1,
              },
              {
                label: "否",
                value: 0,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "偏好",
            prop: "site",
            type: "select",
            editDisplay: false,
            editDisabled: true,
            search: true,
            placeholder: "偏好",
            dicData: [
              {
                label: "男",
                value: 1,
              },
              {
                label: "女",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "作品字数",
            prop: "word_count",
            type: "input",
            required: true,
            editDisplay: false,
            span: 8,
          },
          {
            label: "分类",
            prop: "category_id_1",
            type: "select",
            search: true,
            placeholder: "分类",
            dicData: [],
            props: {
              label: "label",
              value: "value",
            },
            span: 9,
            rules: [
              {
                required: true,
                message: "分类必须选择",
              },
            ],
          },
          {
            label: "作者ID",
            prop: "user_id",
            type: "input",
            search: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "作者名称",
            prop: "author_name",
            type: "input",
            search: true,
            required: true,
            span: 9,
            editDisabled: true,
            rules: [
              {
                required: true,
                message: "作者名称必须填写",
              },
            ],
          },
          {
            label: "千字/分",
            prop: "k_word_price",
            type: "input",
            hide: true,
            search: false,
            required: true,
            span: 9,
            rules: [
              {
                required: true,
                message: "千字/分必须填写",
              },
            ],
          },
          {
            label: "收费章节",
            prop: "pay_n_chapter",
            type: "input",
            hide: true,
            span: 9,
            search: false,
            rules: [
              {
                required: true,
                message: "收费章节必须填写",
              },
            ],
          },
          {
            label: "书籍简介",
            prop: "book_intro",
            type: "textarea",
            hide: true,
            search: false,
            required: true,
            span: 18,
            rules: [
              {
                required: true,
                message: "书籍简介必须填写",
              },
            ],
          },
          {
            label: "书籍状态",
            prop: "is_delete",
            type: "select",
            filter: false,
            // hide: true,
            editDisplay: false,
            editDisabled: true,
            search: true,
            placeholder: "书籍状态",
            dicData: [
              {
                label: "正常",
                value: 0,
              },
              {
                label: "删除",
                value: 1,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "是否上线",
            prop: "is_online",
            type: "select",
            filter: false,
            editDisplay: false,
            editDisabled: true,
            search: true,
            placeholder: "上下线状态",
            dicData: [
              {
                label: "上线",
                value: 1,
              },
              {
                label: "下线",
                value: 0,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "收费状态",
            prop: "is_free",
            type: "select",
            search: true,
            required: true,
            dicData: [
              {
                label: "收费",
                value: 0,
              },
              {
                label: "免费",
                value: 1,
              },
              {
                label: "广告免费",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "最小字数",
            prop: "startWordCount",
            type: "input",
            hide: true,
            search: true,
            editDisplay: false,
            placeholder: "字数区间",
          },
          {
            label: "最大字数",
            prop: "endWordCount",
            type: "input",
            hide: true,
            search: true,
            editDisplay: false,
            placeholder: "字数区间",
          },
          {
            label: "cp方",
            prop: "channel_id",
            type: "select",
            formslot: true,
            editDisplay: false,
            editDisabled: true,
            // search: true,
            required: true,
            filterable: true,
            dicUrl: `${catUrl + appChannelInfo.getList}`,
            placeholder: "cp方",
            dicFormatter: (res) => {
              return res.source;
            },
            dataType: "number",
            props: {
              label: "channel_name",
              value: "channel_id",
            },
          },
          {
            label: "评分",
            prop: "whole",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "评分类型",
            prop: "score_type",
            type: "select",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            search: true,
            dicData: [
              {
                label: "默认评分",
                value: 1,
              },
              {
                label: "用户评分",
                value: 2,
              },
              {
                label: "人工评分",
                value: 3,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "最小评分",
            prop: "min_score",
            type: "number",
            hide: true,
            search: true,
            editDisplay: false,
            placeholder: "字数区间",
          },
          {
            label: "最大评分",
            prop: "max_score",
            type: "number",
            hide: true,
            search: true,
            editDisplay: false,
            placeholder: "字数区间",
          },
          {
            label: "语言类型",
            prop: "language",
            type: "tree",
            editDisplay: false,
            editDisabled: true,
            search: true,
            placeholder: "语言类型",
            dicData: [
              {
                label: "自动检测",
                value: 0,
              },
              {
                label: "中文(简体)",
                value: 1,
              },
              {
                label: "中文(繁体)",
                value: 4,
              },
              {
                label: "中文(粤语)",
                value: 5,
              },
              {
                label: "中文(文言文)",
                value: 6,
              },
              {
                label: "英语",
                value: 2,
              },
              {
                label: "芬兰语",
                value: 3,
              },
              {
                label: "日语",
                value: 7,
              },
              {
                label: "韩语",
                value: 8,
              },
              {
                label: "法语",
                value: 9,
              },
              {
                label: "西班牙语",
                value: 10,
              },
              {
                label: "泰语",
                value: 11,
              },
              {
                label: "阿拉伯语",
                value: 12,
              },
              {
                label: "俄语",
                value: 13,
              },
              {
                label: "葡萄牙语",
                value: 14,
              },
              {
                label: "德语",
                value: 15,
              },
              {
                label: "意大利语",
                value: 16,
              },
              {
                label: "希腊语",
                value: 17,
              },
              {
                label: "荷兰语",
                value: 18,
              },
              {
                label: "波兰语",
                value: 19,
              },
              {
                label: "保加利亚语",
                value: 20,
              },
              {
                label: "爱沙尼亚语",
                value: 21,
              },
              {
                label: "丹麦语",
                value: 22,
              },
              {
                label: "捷克语",
                value: 23,
              },
              {
                label: "罗马尼亚语",
                value: 24,
              },
              {
                label: "斯洛文尼亚语",
                value: 25,
              },
              {
                label: "瑞典语",
                value: 26,
              },
              {
                label: "匈牙利语",
                value: 27,
              },
              {
                label: "越南语",
                value: 28,
              },
              {
                label: "世界语",
                value: 29,
              }
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "cp方",
            prop: "channel_id",
            type: "tree",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
            search: true,
            dicData: [],
            props: {
              label: "channel_name",
              value: "channel_id",
            },
          },
          {
            label: "书籍是否翻译",
            prop: "trans",
            span: "书籍是否翻译",
            width: 120,
            search: true,
            type: "select",
            editDisplay: false,
            dicData: [
              {
                label: "是",
                value: 1,
              },
              {
                label: "否",
                value: 0,
              },
            ],
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: true,
            editDisplay: true,
            addDisabled: true,
            editDisabled: true,
            slot: true,
          },
          {
            label: "是否签约",
            prop: "is_contract",
            type: "radio",
            hide: true,
            dicData: [
              { label: "是", value: 1 },
              { label: "否", value: 0 },
            ],
            addDisplay: true,
            editDisplay: true,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "签约方式",
            prop: "contract_type",
            type: "select",
            hide: true,
            dicData: [
              {
                label: "分成签约",
                value: 0,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            addDisplay: true,
            editDisplay: true,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "分成比例",
            prop: "platform_proportion",
            type: "input",
            display: true,
            hide: true,
            placeholder: "作者 : 平台",
            // slot: true,
            addDisplay: true,
            editDisplay: true,
            addDisabled: false,
            editDisabled: false,
          },
        ],
      },
    };
  },
  created() {
    // this.getList();
  },
  computed: {
    myAction() {
      return this.catUrl + "/file/uploadFile";
    },
    myActions() {
      return this.catUrl + "/bookImport/uploadFile";
    },
    downFile(){
      return this.catUrl + "/bookImport/sample"
    }
  },
  watch: {
    "form.is_contract"(val) {
      if (val == 1) {
        this.findObject(
          this.option.column,
          "platform_proportion"
        ).display = true;
      } else {
        // this.defaults.platform_proportion.display=false
        this.findObject(
          this.option.column,
          "platform_proportion"
        ).display = false;
      }
    },
  },
  methods: {
    async exportExcel() {
      this.loading = true;
      this.dialogVisible = true;
      //处理搜索条件
      let params = { ...this.$refs.crud.searchForm };

      params.route = "内容管理/书籍管理";

      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000;
      if (params.book_id) {
        let bookIdsArr = params.book_id.split(" ");
        params.book_ids = bookIdsArr.join(",");
        export_condition += " &书籍ids：" + params.book_id;
      }
      if (params.cp_book_id) {
        let bookIdsArr = params.cp_book_id.split(" ");
        params.cp_book_ids = bookIdsArr.join(",");
        export_condition += " &cp书籍ids：" + params.cp_book_id;
      }
      if (params.book_title) {
        export_condition += " &书籍名称：" + params.book_title;
      }

      if (params.writing_process) {
        export_condition +=
          " &是否完本：" +
          this.option.column[5].dicData.filter(
            (source) => source.value == params.writing_process
          )[0].label;
      }
      if (params.site) {
        export_condition +=
          " &偏好：" +
          this.option.column[6].dicData.filter(
            (source) => source.value == params.site
          )[0].label;
      }
      if (params.category_id_1) {
        export_condition +=
          " &分类：" +
          this.option.column[8].dicData.filter(
            (source) => source.value == params.category_id_1
          )[0].label;
      }
      if (params.user_id) {
        params.author_id = params.user_id;
        export_condition += " &作者id：" + params.user_id;
      }
      if (params.author_name) {
        export_condition += " &作者名称：" + params.author_name;
      }
      if (params.is_delete) {
        export_condition +=
          " &书籍状态：" +
          this.option.column[14].dicData.filter(
            (source) => source.value == params.is_delete
          )[0].label;
      }
      if (params.is_online) {
        export_condition +=
          " &是否上线：" +
          this.option.column[15].dicData.filter(
            (source) => source.value == params.is_online
          )[0].label;
      }
      if (params.is_free) {
        export_condition +=
          " &收费状态：" +
          this.option.column[16].dicData.filter(
            (source) => source.value == params.is_free
          )[0].label;
      }
      if (params.startWordCount) {
        params.word_count_min = params.startWordCount;
        export_condition += " &最小字数：" + params.startWordCount;
      }
      if (params.endWordCount) {
        params.word_count_max = params.endWordCount;
        export_condition += " &最大字数：" + params.endWordCount;
      }
      if (params.score_type) {
        export_condition +=
          " &评分类型：" +
          this.option.column[21].dicData.filter(
            (source) => source.value == params.score_type
          )[0].label;
      }
      if (params.min_score) {
        params.score_min = params.min_score;
        export_condition += " &书籍最小评分：" + params.min_score;
      }
      if (params.max_score) {
        params.score_max = params.max_score;
        export_condition += " &书籍最大评分：" + params.max_score;
      }

      if (params.language) {
        export_condition +=
          " &语言：" +
          this.option.column[24].dicData.filter(
            (source) => source.value == params.language
          )[0].label;
      }
      if (params.channel_id) {
        params.cp_id = params.channel_id;
        export_condition +=
          " &cp方：" +
          this.option.column[25].dicData.filter(
            (source) => source.channel_id == params.channel_id
          )[0].channel_name;
      }
      if (params.trans) {
        params.trans = params.trans;
        export_condition +=
          " &书籍是否翻译：" +
          this.option.column[26].dicData.filter(
            (source) => source.value == params.trans
          )[0].label;
      }

      params.export_condition =
        export_condition == "" ? "所有" : export_condition;

      //调用导出接口
      let response = await common(
        baseURL.export,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },
    // 导入书籍弹窗
    importBooksBtn() {
      this.importBooksPopup = true;
      this.loadingBtn = false;
    },
    // 导入记录
    importRecordBtn() {
      this.$router.push("/orangecatcontent/booksImport");
    },
    // 书籍导入-- 切换创建方式
    changeType(e){
      this.$refs.ruleForm.resetFields();
      this.ruleForm = {
        type: e, //导入类型 0 创建新书 1 更新书籍
        cover_url: "", //书籍封面
        book_title: "", //书籍名称
        book_intro: "", //书籍简介
        writing_process: "1", //书籍状态
        book_id: "", //书籍ID
        language: "", //语言类型
        site: "", //书籍偏好
        category_id_1: "", //书籍分类
        fileUrl: "", //文件上传
      }
      this.fileName = "";
      this.loadingBtn = false;
    },
    changeFile(file, fileList) {
      // console.log(file);
    },
    changeFiles(file, fileList) {
      // console.log(this.fileName);
      this.fileName = file.name;
      this.loadingBtn = true;
      // console.log(this.fileName)
    },

    beforeUpload(file) {
      // let filename = file.name
      // let arr = filename.split('.')
      // if (arr[1] !== 'plain' && arr[1] !== 'x-zip-compressed') {
      //   this.$message.error(' 仅支持txt的压缩zip文件上传!')
      //   return false
      // }
      // return arr
      console.log(file);
      let isImage = file.type.indexOf("text/plain") != -1;
      let isImagePng = file.type.indexOf("application/x-zip-compressed") != -1;
      // console.log(fileType)
      // 这里常规检验，看项目需求而定
      if (!isImage && !isImagePng) {
        this.$message.error("仅支持txt的压缩zip文件上传!");
        return;
      }
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.loadingBtn = false;
      this.ruleForm.fileUrl = "";
      this.fileName = "";
    },
    successFile(response, file, fileList) {
      let arr = response.source.split(".com/")[1];
      // console.log(arr)
      this.ruleForm.fileUrl = arr;
      this.loadingBtn = false;
      // console.log(this.ruleForm)
    },

    /**头像上传成功 */
    successimage(response, file, fileList) {
      //  console.log(response)
      //   console.log(file)
      this.ruleForm.cover_url = response.source;
    },

    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let params = {};
          if(this.ruleForm.type == 0) {
            if(this.fileName.indexOf(this.ruleForm.book_title) > -1) {
              params = Object.assign({},this.ruleForm)
            } else {
              this.$message.error("书籍名称不一致！");
              return;
            }
          } else if (this.ruleForm.type == 1) {
            params = {
              type: this.ruleForm.type,
              book_id: this.ruleForm.book_id,
              writing_process: this.ruleForm.writing_process
            }
          }
          
          let data = await common(
            bookImport.import,
            { ...params },
            null,
            "POST"
          );
          if (data.source != "原书ID不存在") {
              this.$message({
                message: "导入成功！",
                type: "success",
              });
              this.cancelBtn();
            } else {
              this.$message.error(data.source);
            }

          //  if(this.ruleForm.fileUrl == '') {
          //     this.$message({
          //               message: '书籍正在上传中，请稍后...',
          //               type: 'error'
          //             });
          //     return
          //   }
          // console.log(JSON.stringify(this.ruleForm) )

          // console.log(list)
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 取消
    cancelBtn() {
      this.$refs.ruleForm.resetFields();
      this.ruleForm = {
        type: "0", //导入类型 0 创建新书 1 更新书籍
        cover_url: "", //书籍封面
        book_title: "", //书籍名称
        book_intro: "", //书籍简介
        writing_process: "1", //书籍状态
        book_id: "", //书籍ID
        language: "", //语言类型
        site: "", //书籍偏好
        category_id_1: "", //书籍分类
        fileUrl: "", //文件上传
      },
      this.fileName = "";
      // this.$refs.myUpload.clearFiles()
      this.importBooksPopup = false;
    },

    handleAvatarSuccessx(res) {
      console.log(res.file);
      let params = res.file; // this.$refs.upLoadImg.uploadFiles[0].raw
      this.ruleForm.book_id = URL.createObjectURL(params);
      let formData = new FormData();
      formData.append("file", params);
      uploadFile(formData).then((res) => {
        sessionStorage.setItem("certificatesA", res.source);
      });
    },

    //数据导出
    async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = { ...this.params };

      if (params.book_title) {
        params.book_title = `%${params.book_title}%`;
      }
      let start = params.startWordCount;
      let end = params.endWordCount;

      if (end && start) {
        params.word_count = `=<>${start},${end}`;
      }
      if (start) {
        params.word_count = `>${start}`;
      }
      if (end) {
        params.word_count = `<${end}`;
      }
      delete params.licensed_app_ids;
      delete params.last_chapter_time;

      //调用导出接口
      handleExcelCat(
        this.$export,
        baseURL.getBookList,
        { ...params },
        "书籍管理",
        this.page.total,
        [...this.option.column],
        {
          handleIndexArr: [5, 7, 13, 14, 15],
          selectNameArr: [
            "writing_process",
            "category_id_1",
            "is_delete",
            "is_online",
            "is_free",
          ],
          sourceNameArr: ["value", "value", "value", "value", "value"],
          sourceValueArr: ["label", "label", "label", "label", "label"],
        }
      );
      this.loading = false;
    },
    async getList() {
      this.loading = true;
      this.dialogVisible = false;
      let param = Object.assign(
        {
          pageNum: this.page.currentPage,
          pageSize: this.page.pageSize,
        },
        this.params
      );
      this.data = [];
      let list = await getList(param);
      console.log(list);
      this.pages(list.data.source);
      list = list.data.source.records;
      this.data = list;
      this.loading = false;
    },

    pages(data) {
      this.page.total = data.total;
      this.page.pageSize = data.size;
      this.page.currentPage = data.current;
    },
    async scoreSet(row) {
      let { data } = await common(
        appBookScore.getBookScoreByBookId + "/" + row.book_id,
        null,
        null,
        "GET"
      );
      if (data) {
        this.bookScore = data;
      }
      this.bookScore.book_title = row.book_title;
      this.dialogFormVisible = true;
    },
    updateScore(value, oldValue) {
      console.log(value, oldValue);
      if (value == undefined) {
        if (this.bookScore.whole == undefined) {
          this.bookScore.whole = oldValue;
        }
        if (this.bookScore.plot == undefined) {
          this.bookScore.plot = oldValue;
        }
        if (this.bookScore.character_setting == undefined) {
          this.bookScore.character_setting = oldValue;
        }
        if (this.bookScore.punchline == undefined) {
          this.bookScore.punchline = oldValue;
        }
      }
    },
    async updateScoreSubmit() {
      let userInfo = this.$store.state.user.userInfo;
      let edit_admin_id = userInfo.id;
      let edit_admin_name = userInfo.username;
      let data = await common(
        appBookScore.update,
        { ...this.bookScore, edit_admin_id, edit_admin_name },
        null,
        "POST"
      );
      if (data) {
        this.getList();
        this.dialogFormVisible = false;
      }
    },
    async rowSave(row, done) {},
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      row.is_contract = +row.is_contract;
      row.contract_type = +row.contract_type;
      row.platform_proportion = +row.platform_proportion;
      let data = await updateBook(row);
      this.getList();
      done();
    },
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let key = this.option["rowKey"] ? this.option["rowKey"] : "id";
          let param = { key, value: row[key] };
          let data = await common(
            this.json.crud,
            "delete",
            param,
            null,
            "POST"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    resetForm() {
      this.form = {};
      this.channel_id = "";
    },
    searchChange(params, done) {
      this.condition = [];
      // console.log(this.channel_id,'this.channel_id')
      // let item={channel_id:this.channel_id}
      this.params = { ...params };
      // for (let key in params) {
      //   this.condition.push({ express: key + " like '%" + params[key] + "%'" });
      // }
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    refreshChange() {
      this.getList();
    },
    childlist(scope) {
      this.$router.push("/bookmanager/chapterlist?bookid=" + scope.row.book_id);
    },
  },
  async created() {
    let data = await common(`${bookCategory.getListByLeave}/1`);
    if (data.code == 1) {
      this.option.column[8].dicData = data.data;
      this.classificationList = data.data;
    }
    let reponese = common(appChannelInfo.getList).then((res) => {
      this.option.column[25].dicData = res.data;
    });
  },

  mounted() {
    let reponese = common(appChannelInfo.getList).then((res) => {
      this.options = res.data;
      console.log(res, "sssssssssssssss");
    });
    //  console.log(reponese)
  },
};
</script>

<style scope>
.el-rate {
  display: inline-block;
}
.score-input {
  margin-left: 20px;
}
.create_mode .el-form-item {
  display: flex;
  align-items: center;
}
.create_mode .el-form-item .el-form-item__content {
  margin-left: 0 !important;
}
.cover_boxs .el-upload {
  border: none;
}
.el-upload__tip {
  margin-left: 10px;
}
.el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.cover_box {
  display: flex;
}
.dialog_title {
  margin-right: 10px;
}
.dialog_title_box {
  width: 100%;
  display: flex;
  align-items: center;
  flex: 1;
}
.dialog_title_boxs {
  display: flex;
  align-items: center;
}
.brief .el-form-item {
  width: 100%;
}
.cover_boxs_textarea .dialog_title_box .el-textarea {
  flex-shrink: 0;
  width: 100%;
}
.cover_boxs_textarea {
  width: 100%;
  display: flex;
  align-items: center;
}
.cover_boxs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20px;
}
.cover_boxs .el-input {
  width: 100%;
}
.el-upload-list__item:first-child {
  margin: 0;
}
.el-form-item__content {
  line-height: 0 !important;
}
.file_name {
  display: flex;
  align-items: center;
  margin-top: 12px;
}
.file_name i {
  cursor: pointer;
  margin-left: 10px;
}
</style>